package cn.stylefeng.roses.kernel.pay.modular.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.pay.api.exception.PayException;
import cn.stylefeng.roses.kernel.pay.api.exception.enums.GoodsExceptionEnum;
import cn.stylefeng.roses.kernel.pay.modular.entity.Goods;
import cn.stylefeng.roses.kernel.pay.modular.mapper.GoodsMapper;
import cn.stylefeng.roses.kernel.pay.modular.pojo.GoodsRequest;
import cn.stylefeng.roses.kernel.pay.modular.service.GoodsService;
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品信息业务实现层
 *
 * @author stylefeng
 * @date 2021/06/23 22:28
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Override
    public List<Goods> findList(GoodsRequest goodsRequest) {
        LambdaQueryWrapper<Goods> wrapper = this.createWrapper(null);
        return this.list(wrapper);
    }

    @Override
    public Goods detail(GoodsRequest goodsRequest) {
        return this.queryGoods(goodsRequest);
    }

    @Override
    public void add(GoodsRequest goodsRequest) {
        Goods goods = new Goods();
        BeanUtil.copyProperties(goodsRequest, goods);
        this.save(goods);
    }

    @Override
    public void del(GoodsRequest goodsRequest) {
        Goods goods = this.queryGoods(goodsRequest);
        this.removeById(goods.getGoodsId());
    }

    @Override
    public void edit(GoodsRequest goodsRequest) {
        Goods goods = this.queryGoods(goodsRequest);
        BeanUtil.copyProperties(goodsRequest, goods);
        this.updateById(goods);
    }

    @Override
    public PageResult<Goods> findPage(GoodsRequest goodsRequest) {
        LambdaQueryWrapper<Goods> wrapper = createWrapper(goodsRequest);
        Page<Goods> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    /**
     * 获取信息
     *
     * @author stylefeng
     * @date 2021/06/23 22:28
     */
    private Goods queryGoods(GoodsRequest goodsRequest) {
        Goods goods = this.getById(goodsRequest.getGoodsId());
        if (ObjectUtil.isEmpty(goods)) {
            throw new PayException(GoodsExceptionEnum.GOODS_NOT_EXISTED);
        }
        return goods;
    }

    /**
     * 创建查询wrapper
     *
     * @author stylefeng
     * @date 2021/06/23 22:28
     */
    private LambdaQueryWrapper<Goods> createWrapper(GoodsRequest goodsRequest) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();

        // 按顺序正序排列
        queryWrapper.orderByAsc(Goods::getSortNumber);

        // 只查询上架的商品
        queryWrapper.eq(Goods::getGoodsStatus, StatusEnum.ENABLE.getCode());

        if (goodsRequest == null) {
            return queryWrapper;
        }

        Long goodsId = goodsRequest.getGoodsId();
        String goodsName = goodsRequest.getGoodsName();
        String goodsDesc = goodsRequest.getGoodsDesc();
        Integer goodsStatus = goodsRequest.getGoodsStatus();

        queryWrapper.eq(ObjectUtil.isNotNull(goodsId), Goods::getGoodsId, goodsId);
        queryWrapper.like(ObjectUtil.isNotEmpty(goodsName), Goods::getGoodsName, goodsName);
        queryWrapper.like(ObjectUtil.isNotEmpty(goodsDesc), Goods::getGoodsDesc, goodsDesc);
        queryWrapper.eq(ObjectUtil.isNotNull(goodsStatus), Goods::getGoodsStatus, goodsStatus);

        return queryWrapper;
    }

}
